package com.it.gulimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it.common.utils.PageUtils;
import com.it.common.utils.Query;

import com.it.gulimall.product.dao.CategoryDao;
import com.it.gulimall.product.entity.CategoryEntity;
import com.it.gulimall.product.service.CategoryService;

/**
 * @author yeah
 * @date 2020年5月6日18点13分
 *
 * 源码解析
 * 1、CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity>
 *      ServiceImpl<M extends BaseMapper<T>, T> implements IService<T>
 *              @Autowired
 *              protected M baseMapper;
 *        public interface BaseMapper<T> extends Mapper<T>
 *      由此看出传入的 CategoryDao 继承 BaseMapper 也继承了Mapper
 */

@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * queryWrapper 实体对象封装操作类（可以为 null）
     * @return
     *         // 组装成为父子结构的分类信息
     *         List<CategoryEntity> collectEntityList = categoryEntityList.stream().filter(
     *                 (categoryEntity) -> categoryEntity.getParentCid() == 0
     *         ).collect(Collectors.toList());
     */
    @Override
    public List<CategoryEntity> listWithTree() {
        // 获取分类的全部信息
        List<CategoryEntity> categoryEntityList = baseMapper.selectList(null);
        // 一级分类总和
        List<CategoryEntity> entityListLevel1 = categoryEntityList.stream().filter(
                CategoryEntity -> CategoryEntity.getCatLevel() == 1)
                .collect(Collectors.toList());
        // 二级分类总和
        List<CategoryEntity> entityListLevel2 = categoryEntityList.stream().filter(
                CategoryEntity -> CategoryEntity.getCatLevel() == 2)
                .collect(Collectors.toList());
        // 三级分类总和
        List<CategoryEntity> entityListLevel3 = categoryEntityList.stream().filter(
                CategoryEntity -> CategoryEntity.getCatLevel() == 3)
                .collect(Collectors.toList());
        // 设置一级分类的子分类
        for (CategoryEntity categoryEntity1 : entityListLevel1){
            List<CategoryEntity> tempCategoryList = new ArrayList<>();
            for (CategoryEntity categoryEntity2 : entityListLevel2){
                if (categoryEntity1.getCatId().equals(categoryEntity2.getParentCid())){
                    tempCategoryList.add(categoryEntity2);
                }
            }
            categoryEntity1.setChildren(tempCategoryList);
        }
        // 设置二级分类的子分类
        for (CategoryEntity categoryEntity2 : entityListLevel2){
            List<CategoryEntity> tempCategoryList = new ArrayList<>();
            for (CategoryEntity categoryEntity3 : entityListLevel3){
                if (categoryEntity2.getCatId().equals(categoryEntity3.getParentCid())){
                    tempCategoryList.add(categoryEntity3);
                }
            }
            categoryEntity2.setChildren(tempCategoryList);
        }
        return entityListLevel1;
//        // 获取分类的全部信息
//        List<CategoryEntity> categoryEntityList = baseMapper.selectList(null);
//        // 一级分类总和
//        List<CategoryEntity> entityListLevel1 = categoryEntityList.stream().filter(
//                CategoryEntity -> CategoryEntity.getCatLevel() == 1)
//                .map(menu ->{menu.setChildren(getChildrenList(menu, categoryEntityList));
//                return menu;
//                })
//                .sorted((menu1, menu2) ->{
//                    return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());})
//                .collect(Collectors.toList());
//
//
//        return entityListLevel1;
    }
//    public List<CategoryEntity> getChildrenList(CategoryEntity parenEntity , List<CategoryEntity> all){
//        List<CategoryEntity> children = all.stream().filter(categoryEntity -> parenEntity.getCatId().equals(categoryEntity.getParentCid()))
//                .map(categoryEntity -> {
//                    categoryEntity.setChildren(getChildrenList(categoryEntity, all));
//                    return categoryEntity;
//                })
//                .sorted((menu1, menu2) ->{
//                    return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());})
//                .collect(Collectors.toList());
//        return children;
//    }

}